﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace BlackJack
{
    public partial class GamePage : PhoneApplicationPage
    {
        #region Private
        private const int m_numOfDecks = 3;
        private const int m_numOfPlayers = 1;

        private Player[] m_player;
        private Dealer m_dealer;
        private DealingShoe m_shoe;
        #endregion

        #region Methods
        public GamePage()
        {
            InitializeComponent();
            m_player = new Player[m_numOfPlayers];
            for(int i = 0; i < m_numOfPlayers; i++)
            {
                m_player[i] = new Player();
            }
            m_dealer = new Dealer();
            m_shoe = new DealingShoe(m_numOfDecks);
            m_shoe.EndOfShoeEvent += new DealingShoe.ShoeEventHandler(Shoe_EndOfShoeEvent);
            m_shoe.ShuffleEvent +=new DealingShoe.ShoeEventHandler(Shoe_ShuffleEvent);
            int cardRow = 2;
            int cardCol = 1;
            Card newCard = new Card(Card.CardType.Ace, Card.CardSuit.Clubs);
            Image cardImg = newCard.DrawCard();
            cardImg.SetValue(Grid.RowProperty, cardRow);
            cardImg.SetValue(Grid.ColumnProperty, cardCol);
            ContentPanel.Children.Add(cardImg);
            Card newCard2 = new Card(Card.CardType.Ace, Card.CardSuit.Diamonds);
            Image cardImg1 = newCard2.DrawCard();
            cardImg1.SetValue(Grid.RowProperty, cardRow);
            cardImg1.SetValue(Grid.ColumnProperty, 2);
            ContentPanel.Children.Add(cardImg1); 
        }

        public void Shoe_EndOfShoeEvent(Object obj, EventArgs e)
        {
            m_shoe.Shuffle();
        }

        public void Shoe_ShuffleEvent(Object obj, EventArgs e)
        {

        }

        #endregion

        private void dealButton_Click(object sender, RoutedEventArgs e)
        {
            ContentPanel.Children.Clear();
            Rectangle[] cardRect;
            //Reset the player
            foreach (Player p in m_player)
            {
                p.Reset();
            }

            //reset the dealer
            m_dealer.ResetHand();

            if (m_shoe.Eod)
            {
                //Shuffle the dealing shoe
                m_shoe.Shuffle();
            }

            //deal 2 cards to every player and dealer
            for (int i = 0; i < 2; i++)
            {
                foreach (Player p in m_player)
                {
                    //set the bet
                    p.SetBet();                   
                    p.CurrentHand().Add(m_shoe.NextCard());
                }
                m_dealer.DealerHand.Add(m_shoe.NextCard());
            }
            m_dealer.DealerHand.HandLocation = new Thickness(200, 50, 0, 0);
            //cardRect = m_dealer.DealerHand.DrawHand();
            //foreach (Rectangle r in cardRect)
            //{
            //    if(r != null)
            //        ContentPanel.Children.Add(r);
            //}
            //Draw the hands on the screen.
            foreach (Player p in m_player)
            {
                //set the margin where to draw the hand for the player
                p.CurrentHand().HandLocation = new Thickness(200, 500, 0, 0);
                //p.CurrentHand().DrawHand(ContentPanel);
            }
        }
    }
}